/**
 * Copyright (c) 2007-2014 The Apereo Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             http://opensource.org/licenses/ecl2
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
* Licensed to The Apereo Foundation under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* The Apereo Foundation licenses this file to you under the Educational 
* Community License, Version 2.0 (the "License"); you may not use this file 
* except in compliance with the License. You may obtain a copy of the 
* License at:
*
* http://opensource.org/licenses/ecl2.txt
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.sakaiproject.signup.impl.messages;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.sakaiproject.signup.api.SakaiFacade;
import org.sakaiproject.signup.api.model.SignupMeeting;
import org.sakaiproject.signup.api.model.SignupTimeslot;
import org.sakaiproject.user.api.User;

/**
 * Represents an email notification sent to an attendee when they sign themselves up for a meeting timeslot.
 * Extends {@link AttendeeEmailBase} to provide specific email content for self-signup scenarios.
 *
 * <p>This class handles:
 * <ul>
 *   <li>Email formatting and content generation for self-signup confirmations</li>
 *   <li>Includes meeting details, timeslot information, and any attendee comments</li>
 *   <li>Uses appropriate localization for message content</li>
 * </ul>
 * </p>
 *
 * @see AttendeeEmailBase
 * @see SignupMeeting
 * @see SignupTimeslot
 */
public class AttendeeSignupOwnEmail extends AttendeeEmailBase {

	private final User attendee;
	private final SignupTimeslot timeslot;

	/**
	 * @param attendee User who just signed up
	 * @param signupMeeting the SignupMeeting they signed up to
	 * @param timeslot the SignupTimeslot they signed up to
	 * @param sakaiFacade a SakaiFacade object
	 */
	public AttendeeSignupOwnEmail(User attendee, SignupMeeting signupMeeting, SignupTimeslot timeslot, SakaiFacade sakaiFacade) {
		this.attendee = attendee;
		this.meeting = signupMeeting;
		this.timeslot = timeslot;
		this.setSakaiFacade(sakaiFacade);
	}

    @Override
	public List<String> getHeader() {
		List<String> rv = new ArrayList<>();
		rv.add("Content-Type: text/html; charset=UTF-8");
		rv.add("Subject: " + getSubject());
		rv.add("From: " + getFromAddress());
		rv.add("To: " + attendee.getEmail());

		return rv;
	}

    @Override
	public String getMessage() {
		StringBuilder message = new StringBuilder();
		message.append(MessageFormat.format(rb.getString("body.top.greeting.part"), makeFirstCapLetter(attendee.getDisplayName())));

		message.append(NEWLINE).append(NEWLINE).append(MessageFormat.format(
                rb.getString("body.attendee.signup.own"),
                getSiteTitleWithQuote(),
                getServiceName()));

		message.append(NEWLINE).append(NEWLINE).append(MessageFormat.format(
                rb.getString("body.meetingTopic.part"), meeting.getTitle()));
		if (!meeting.isMeetingCrossDays()) {
			Object[] paramsTimeframe = new Object[] {
                    getTime(timeslot.getStartTime()).toStringLocalTime(),
					getTime(timeslot.getEndTime()).toStringLocalTime(),
					getTime(timeslot.getStartTime()).toStringLocalDate(),
					getSakaiFacade().getTimeService().getLocalTimeZone().getID()
            };
			message.append(NEWLINE).append(MessageFormat.format(
                    rb.getString("body.attendee.meeting.timeslot"), paramsTimeframe));
		} else {
			Object[] paramsTimeframe = new Object[] {
                    getTime(timeslot.getStartTime()).toStringLocalTime(),
					getTime(timeslot.getStartTime()).toStringLocalShortDate(),
					getTime(timeslot.getEndTime()).toStringLocalTime(),
					getTime(timeslot.getEndTime()).toStringLocalShortDate(),
					getSakaiFacade().getTimeService().getLocalTimeZone().getID()
            };
			message.append(NEWLINE).append(MessageFormat.format(
                    rb.getString("body.attendee.meeting.crossdays.timeslot"), paramsTimeframe));

		}
		
		// attendee's comment
        if (timeslot.getAttendee(attendee.getId()) != null) {
            String comments = StringUtils.trimToEmpty(timeslot.getAttendee(attendee.getId()).getComments());
            if (StringUtils.isNotBlank(comments) && !comments.equals("&nbsp;")) {
                message.append(NEWLINE)
                        .append(NEWLINE)
                        .append(StringEscapeUtils.escapeHtml4(MessageFormat.format(rb.getString("body.ownComment"), comments)));
            }
        }

        // footer
		message.append(NEWLINE).append(getFooter(NEWLINE));
		return message.toString();
	}
	
	@Override
	public String getFromAddress() {
		return getServerFromAddress();
	}

	@Override
	public String getSubject() {
		return MessageFormat.format(rb.getString("subject.attendee.signup.own.field"), getAbbreviatedMeetingTitle(), getSiteTitle());
	}
	
}
